// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   ProfileManager.java

package jeeves.server;

import java.util.*;
import jeeves.interfaces.Logger;
import jeeves.utils.Log;
import jeeves.utils.Xml;
import org.fao.geonet.constants.Geonet;
import org.jdom.Attribute;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;

public class ProfileManager
{
	static{
		System.out.println("============== Profile manager helbert");
        
	}
    public ProfileManager(String profilesFile)
        throws Exception
    {
        Element elProfiles = Xml.loadFile(profilesFile);
        htProfiles = new Hashtable(50);
        List profList = elProfiles.getChildren("profile");
        for(int i = 0; i < profList.size(); i++)
        {
            
            Element elProfile = (Element)profList.get(i);
            String sName = elProfile.getAttributeValue("name");
            htProfiles.put(sName, elProfile);
        }

    }

    public boolean exists(String profile)
    {
        return htProfiles.containsKey(profile);
    }
    
    public Element getAllProfiles(){
    	Element elResult = new Element("profiles");
        for (String name : htProfiles.keySet()) {
            elResult.addContent(new Element(name));
        }
        return elResult;
    }

    public Element getProfilesElement(String profile)
    {
        Set set = getProfilesSet(profile);
        Element elResult = new Element("profiles");
        Iterator i = set.iterator();
        do
        {
            if(!i.hasNext())
                break;
            profile = (String)i.next();
            if(!profile.equals("Guest"))
                elResult.addContent(new Element(profile));
        } while(true);
        return elResult;
    }
   public Set getAllProfileSet(){
       return htProfiles.keySet();
   }

    public Set getProfilesSet(String profile)
    {
        HashSet hs = new HashSet();
        ArrayList al = new ArrayList();
        al.add(profile);
        do
        {
            if(al.isEmpty())
                break;
            profile = (String)al.get(0);
            al.remove(0);
            hs.add(profile);
            Element elProfile = (Element)htProfiles.get(profile);
            String extend = elProfile.getAttributeValue("extends");
            if(extend != null)
            {
                StringTokenizer st = new StringTokenizer(extend, ",");
                while(st.hasMoreTokens()) 
                    al.add(st.nextToken().trim());
            }
        } while(true);
        return hs;
    }

    public Element getAccessibleServices(String profile)
    {
        HashSet hs = new HashSet();
        ArrayList al = new ArrayList();
        al.add(profile);
        do
        {
            if(al.isEmpty())
                break;
            profile = (String)al.get(0);
            al.remove(0);
            Element elProfile = (Element)htProfiles.get(profile);
            List allowList = elProfile.getChildren("allow");
            for(int i = 0; i < allowList.size(); i++)
            {
                Element elAllow = (Element)allowList.get(i);
                String sService = elAllow.getAttributeValue("service");
                hs.add(sService);
            }

            String extend = elProfile.getAttributeValue("extends");
            if(extend != null)
            {
                StringTokenizer st = new StringTokenizer(extend, ",");
                while(st.hasMoreTokens()) 
                    al.add(st.nextToken().trim());
            }
        } while(true);
        Element elRes = new Element("services");
        String service;
        for(Iterator i = hs.iterator(); i.hasNext(); elRes.addContent((new Element("service")).setAttribute(new Attribute("name", service))))
            service = (String)i.next();

        return elRes;
    }

    public boolean hasAccessTo(String profile, String service)
    {
        ArrayList al = new ArrayList();
        al.add(profile);
        do
        {
            if(al.isEmpty())
                break;
            profile = (String)al.get(0);
            al.remove(0);
            Element elProfile = (Element)htProfiles.get(profile);
            List allowList = elProfile.getChildren("allow");
            for(int i = 0; i < allowList.size(); i++)
            {
                Element elAllow = (Element)allowList.get(i);
                String sService = elAllow.getAttributeValue("service");
                if(service.equals(sService))
                    return true;
            }

            String extend = profile = elProfile.getAttributeValue("extends");
            if(extend != null)
            {
                StringTokenizer st = new StringTokenizer(extend, ",");
                while(st.hasMoreTokens()) 
                    al.add(st.nextToken().trim());
            }
        } while(true);
        return false;
    }

    public static final String GUEST = "Guest";
    public static final String ADMIN = "Administrator";
    private Hashtable<String, Element> htProfiles;
}
